#!/bin/bash

showdirs(){
	dirlist=`dirs -l`
	index=0
	for tr in $dirlist
	do
		echo "($index) $tr"
		index=`expr $index + 1 `
	done
}

getfullname(){
	here=`pwd`
	cd $1
	pwd
	cd $here
}

skiptop(){
	shift 1
	echo $*
}

searchdirs(){
	obj=`getfullname $1 | sed "s/\//_/g"`
	showdirs |sed "s/\//_/g"| awk '$2~/^'$obj'$/{print $1}' |sed "s/[(]//" | sed "s/[)]//"
	
}

searchdirs_likely(){
	obj=`echo $1 | sed "s/\//_/g"`
	showdirs |sed "s/\//_/g"| grep "$obj$" |awk '{print $1}'|sed "s/[(]//" | sed "s/[)]//"
}

echo -----$1

if [ ! "$1" = "" ];then
	if [  -d $1 -o  -L $1 ];then
		index=`searchdirs $1`
	   	if [ ! "$index" = "" ];then
			   rundant=`skiptop $index`
			   if [ ! "$rundant" = "" ];then
					ct=0
					for tr in $rundant
					do
						tr=`expr $tr - $ct `
						popd +$tr >/dev/null
						ct=`expr $ct + 1 `
					done
			   fi
			   pushd +$index 1>/dev/null
	   	else
			   pushd $1 1>/dev/null
	   	fi
   	else
		index_likely=`searchdirs_likely $1`	
#		echo index_likely before----$index_likely
		index_likely=`echo $index_likely |awk '{print $1}'`
#		echo index_likely after ----$index_likely
		if [ "$index_likely" == "" ];then
			pushd +$1 1>/dev/null  2>&1
		else
			pushd +$index_likely 1>/dev/null
		fi
   	fi
fi

showdirs

echo -----
ls
